/******************************************************************************

				版权所有 (C), 2023, 威海众鼎智能科技有限公司

 ******************************************************************************
  文 件 名	: User.c
  版 本 号	: V1.0
  硬件信息	: ZHD_039
  作	者	: ZhangFY
  生成日期	: 2023.10.16
  功能描述	: 山沃液力变矩器叉装车主控制器
  修改历史
  1.日    期   :
	作    者   :
	修改内容   :
******************************************************************************/

#include "User.h"
u16 count_id, init_bb, time_delay; // 消息ID
u8 m_wait_gps = 0;				   // 请求数据后等网络恢复数据
u8 m1, sys_tx_gps[100], sys_rx_gps[100], len_sys_gps;
u8 sys_rx_ic[50], len_sys_ic;
u8 sys_rx_print[50], len_sys_print;
u8 delay_opt;
u8 LW_Enable;
u8 motor_x[6], motor_y[6], JF_SUB[6];
u32 JF_user;
u8 sys_tx_qdb[30], sys_rx_qdb[30], len_sys_qdb, arry_m[50], sys_rx_ic_m[30];
u16 time_cyc = 1500; // 未接收到请求循环再次发送数据间隔 10ms
__int64 ID;
//------------------------------------------全局变量 GLOBAL_VAR--------------------------------
u16 Dec_Option(u16 in, u16 par_dec, u16 par_delay, u16 *p);
ZHD_024IO ZHD_024_IN, ZHD_024_OUT;
u8 Tx_020[8], Tx_SL[8];
u8 Rx_010[8], Rx_011[8];
/** 输入 **/
u16 YouGanQiang_YaLi_mA; // 有杆腔压力传感器 mA 值
u16 WuGanQiang_YaLi_mA;	 // 无杆腔压力传感器 mA 值
u16 DangWei_QianHou_mV;	 // 前后挡拨轮
u8 DangWei_N;			 // N 档按钮
u8 DangWei_M;			 // M 档按钮
u8 Button_6;			 //
u16 ShouBing_Y_mV;		 // 手柄 Y 轴 mV 值
u16 ShouBing_X_mV;		 // 手柄 Y 轴 mV 值
u16 ShenSuo_BL_mV;		 // 伸缩拨轮 mV 值
u16 FuJu_BL_mV;			 // 辅具拨轮 mV 值
u8 SiLun_MoShi_SB;		 // 四轮模式开关
u8 XieXing_MoShi_SB;	 // 蟹行模式开关
u8 QianQiao_SB_1;		 // 前桥接近开关
u8 HouQiao_SB_1;		 // 后桥接近开关
u8 YQ_Xie, YQ_Si;		 // 四轮转向，蟹行越权
u8 DW_QianJin;			 // 前进挡开关
u8 DW_HouTui;			 // 后退挡开关
u8 ShouDong_YQ = 0;		 // 转向系统手动越权
u8 ShaChe_XH;			 // 刹车信号
u16 Eng_SP_Freq;		 // 发动机转速传感器频率
u16 Eng_Speed;			 // 发动机转速RPM
u8 DiSu_MoShi = 1;		 // 变矩器低速模式
/** 输出 **/			 //
u16 FuJu_Valve_a;		 // 辅具阀a
u16 FuJu_Valve_b;		 // 辅具阀b
u16 Boom_UP;			 // 大臂升
u16 Boom_DOWN;			 // 大臂降
u16 FanDou_UP;			 // 翻斗
u16 FanDou_DOWN;		 // 收斗
u16 Boom_Shen;			 // 大臂伸出
u16 Boom_Suo;			 // 大臂缩回
u8 SiLun_Fa;			 // 四轮转向阀
u8 XieXing_Fa;			 // 蟹行转向阀
u8 LP_LiangLun;			 // 两轮转向模式指示灯
u8 LP_LiangLun_S;		 // 两轮转向模式指示灯
u8 LP_SiLun;			 // 四轮转向模式指示灯
u8 LP_SiLun_S;			 // 四轮转向模式指示灯
u8 LP_XieXing;			 // 蟹行转向模式指示灯
u8 LP_XieXing_S;		 // 蟹行转向模式指示灯
u8 QianJin_Valve;		 // 变矩器前进挡电磁阀
u8 HouTui_Valve;		 // 变矩器后退挡电磁阀
u8 GaoDiSu_Valve;		 // 变矩器高低速电磁阀
u8 Eng_Start_Protect;	 // 发动机启动保护
u8 D1, D2, N, R1, R2;	 //
u16 System_Voltage;		 // 系统电压
u8 CAN_n;			 // CAN发送循环计数
//------------------------------------------END_VAR--------------------------------
//===============================上升沿===========================================
u8 R_Trig(u8 in, u8 *p)
{
	if (in == 1 && (*p) == 0)
	{
		*p = in;
		return 1;
	}
	else
	{
		*p = in;
		return 0;
	}
}
//================================END_上升沿==========================================
//------------------------------------------FLASH(EEPROM)-------------------------------------
#define FLASH_SAVE_ADDR 0X08040000 // 设置FLASH 保存地址(必须为偶数，且所在扇区,要大于本代码所占用到的扇区.
								   // 否则,写操作的时候,可能会导致擦除整个扇区,从而引起部分程序丢失.引起死机.
u8 flash_byte[500];
u8 eeprom_En[1005], eeprom_En_TD[3];
u16 Nub_eeprom, total_eeprom, Nub_Print, mark_read, count_id2 = 111, CYC_G;
//
u8 Key_Ctrl_L, Key_Ctrl_R, Key_Ctrl_Enable, Pot_Ctrl, Mode_Ctrl;
//
u8 p_u8[15], CAN_ERR;
int p_int[15];
//
// u16 Joy_Fliter(u16 IN,u16 Par_min,u16 Par_max);

// #########################1.系统初始化##############################
int KOP_Init(void)
{
	// 3.CAN0口配置
	CAN1_Mode_Init(250);
	// 4.CAN1口配置
	CAN2_Mode_Init(250);
	return (200);
}
void CAN1_RX_Init(void)
{
	can_receive_setting2(0x010, CAN_ID_STD, 0, CAN_Filter_FIFO0);
}
void CAN2_RX_Init(void)
{
	can_receive_setting2(0x010, CAN_ID_STD, 14, CAN_Filter_FIFO0);
	can_receive_setting2(0x011, CAN_ID_STD, 15, CAN_Filter_FIFO0);
}
void IO_Congig(void)
{
	// 端口配置
	// 配置 < 0:0-5v 1:0-20mA 2:DI+ 3:DI- >
	ECU.PIN_3.Type = 1; // 有杆腔压力传感器
	ECU.PIN_4.Type = 1; // 无杆腔压力传感器
	ECU.PIN_5.Type = 0; // 手柄前进后退档拨轮
	ECU.PIN_6.Type = 2; // 手柄 N 按钮
	ECU.PIN_7.Type = 2; // 手柄 M 按钮
	/*
	 ** ECU.PIN_8.Type = 0; // 手柄 Y 轴
	 ** ECU.PIN_9.Type = 0; // 手柄 X 轴
	 ** ECU.PIN_16.Type = 0; // 手柄伸缩拨轮
	 ** ECU.PIN_17.Type = 0; // 手柄辅具拨轮
	 */
	// PWM_DO_DI 	< 0:DO 1:PWM 2:DI >
	ECU.PIN_2.Type = 1;	 // 辅具阀线圈a
	ECU.PIN_10.Type = 1; // 大臂阀举升
	ECU.PIN_11.Type = 1; // 大臂阀放下
	ECU.PIN_12.Type = 1; // 翻斗阀翻斗
	ECU.PIN_13.Type = 1; // 翻斗阀收斗
	ECU.PIN_25.Type = 1; // 大臂阀伸出
	ECU.PIN_26.Type = 1; // 大臂阀缩回
	ECU.PIN_29.Type = 1; // 辅具阀线圈b
	ECU.PIN_38.Type = 0; // 大臂平衡阀切断阀
	ECU.PIN_39.Type = 0; //
	// PI/DI		< 0:DI 1:PI >
	ECU.PIN_14.Type = 0; //
	ECU.PIN_15.Type = 0; //
	ECU.PIN_18.Type = 1; //
	ECU.PIN_19.Type = 0; //
	/*
	//DI/AI     <PIN_8,PIN_9,PIN_16,PIN_17>
	无需配置，采集AI程序自动转为DI
		ECU.PIN_16				//四轮转向模式开关
		ECU.PIN_17				//蟹行转向模式开关
	*/
	// FB/DO-/DI< u8 Type;//0:DO 1:DI 2:FB >
	ECU.PIN_23.Type = 1; //
	ECU.PIN_24.Type = 1; //
	ECU.PIN_30.Type = 1;
	ECU.PIN_31.Type = 1;
	ECU.PWM_25_26_Set_PI.Par_PI = 200;
	ECU.PWM_12_13_10_11_Set_PI.Par_PI = 200;
	ECU.PWM_2_29_38_39_Set_PI.Par_PI = 200;
}
// ##########################2.主循环##################################
void KOP_Cycle(void)
{
	static u8 MID1, MID2, MID3, MID4;											   //
	static u8 DW_MID1, DW_MID2, DW_NMID, DiSu_MID;								   //
	static u8 XIE_SI, Si_Xie_Liang;												   //
	static u16 SL_count, /* Boom_BH_count, */ YQ_Xie_count, YQ_Si_count, YQ_count; //
	u16 Valve_Current_Boom_UP;													   // 大臂升电流值
	u16 Valve_Current_Boom_DOWN /*, Valve_Current_Boom_DOWN1*/;					   // 大臂降电流值
	u16 Valve_Current_Boom_Shen;												   // 大臂伸出电流值
	u16 Valve_Current_Boom_Suo;													   // 大臂缩回电流值
	u16 Valve_Current_FanDou;													   // 翻斗电流值
	u16 Valve_Current_ShouDou;													   // 收斗电流值
	u16 Valve_Current_FuJu_a;													   // 辅具a电流值
	u16 Valve_Current_FuJu_b;													   // 辅具b电流值
	u8 PingHengFa_QD;															   // 平衡阀切断阀
	u16 ARR1[5];																   // 大臂下降减速斜坡
	static u16 DW_YS_count, DW_YS_count1;										   // 换挡延时计数
	static u8 DW_YS;
	static u8 DW_YS1; // 换挡延时计数
	/*===================输入采集========================*/
	YouGanQiang_YaLi_mA = ECU.PIN_3.AI;
	WuGanQiang_YaLi_mA = ECU.PIN_4.AI;
	DangWei_QianHou_mV = ECU.PIN_5.AI;
	DangWei_N = ECU.PIN_6.DI;
	DangWei_M = ECU.PIN_7.DI;
	ShouBing_Y_mV = ECU.PIN_8.AI;
	ShouBing_X_mV = ECU.PIN_9.AI;
	ShenSuo_BL_mV = ECU.PIN_16.AI;
	FuJu_BL_mV = ECU.PIN_17.AI;
	Eng_SP_Freq = ECU.PIN_18.PI;
	Button_6 = ECU.PIN_23.DI;
	SiLun_MoShi_SB = ZHD_024_IN.PIN_12.DI;
	XieXing_MoShi_SB = ZHD_024_IN.PIN_13.DI;
	QianQiao_SB_1 = ZHD_024_IN.PIN_18.DI;
	HouQiao_SB_1 = ZHD_024_IN.PIN_20.DI;
	ShaChe_XH = ZHD_024_IN.PIN_19.DI;
	System_Voltage = ECU.Volt_Sys * 100;
	/*==================输入采集转换=======================*/
	Eng_Speed = AmplitudeLimiting_Filter((Eng_SP_Freq * 60 / 130), 2200);
	/*****************************
	 * @file 四轮转向
	 * @brief 四轮转向逻辑
	 *****************************/
	// 越权模式
	if (R_Trig(XieXing_MoShi_SB, &MID1))
	{
		ShouDong_YQ = 1;
		YQ_Xie_count++;
	}
	if (YQ_Xie_count == 3)
	{
		YQ_Xie = 1;
	}
	if (YQ_Xie)
	{
		YQ_Xie = 0;
		ECU.FeEL[11] = 2;
		YQ_Xie_count = 0;
	}

	if (R_Trig(SiLun_MoShi_SB, &MID2))
	{
		ShouDong_YQ = 1;
		YQ_Si_count++;
	}
	if (YQ_Si_count == 3)
	{
		YQ_Si = 1;
	}
	if (YQ_Si)
	{
		YQ_Si = 0;
		ECU.FeEL[11] = 1;
		YQ_Si_count = 0;
	}

	if (ShouDong_YQ)
	{
		YQ_count++;
		if (YQ_count == 300)
		{
			ShouDong_YQ = 0;
			YQ_count = 0;
			YQ_Si_count = 0;
			YQ_Xie_count = 0;
		}
	}

	// 正常模式
	if (R_Trig(XieXing_MoShi_SB, &MID3) == 1 || R_Trig(SiLun_MoShi_SB, &MID4) == 1)
	{
		XIE_SI = 1;
	}
	if (XIE_SI == 1)
	{
		if (HouQiao_SB_1 == 1)
		{
			XieXing_Fa = 0;
			SiLun_Fa = 0;
			XIE_SI = 0;
		}
	}
	// 四轮模式切换逻辑
	if (SiLun_MoShi_SB == 1)
	{
		if (ECU.FeEL[11] == 1)
		{
			SiLun_Fa = 1;
			XieXing_Fa = 0;
		}
		else if (ECU.FeEL[11] == 2)
		{
			if (HouQiao_SB_1 != 1)
			{
				SiLun_Fa = 0;
				XieXing_Fa = 1;
			}
			else
			{
				SiLun_Fa = 0;
				XieXing_Fa = 0;
			}
		}
		if (QianQiao_SB_1 == 1 && HouQiao_SB_1 == 1)
		{
			ECU.FeEL[11] = 1; // 四轮转向模式掉电保存
			SiLun_Fa = 1;
			XieXing_Fa = 0;
		}
	}
	// 蟹行模式切换逻辑
	if (XieXing_MoShi_SB == 1)
	{
		if (ECU.FeEL[11] == 2)
		{
			SiLun_Fa = 0;
			XieXing_Fa = 1;
		}
		else if (ECU.FeEL[11] == 1)
		{
			if (HouQiao_SB_1 != 1)
			{
				SiLun_Fa = 1;
				XieXing_Fa = 0;
			}
			else
			{
				SiLun_Fa = 0;
				XieXing_Fa = 0;
			}
		}
		if (QianQiao_SB_1 == 1 && HouQiao_SB_1 == 1)
		{
			ECU.FeEL[11] = 2; // 蟹行转向模式掉电保存
			XieXing_Fa = 1;
			SiLun_Fa = 0;
		}
	}
	if (SiLun_MoShi_SB == 0 && XieXing_MoShi_SB == 0)
	{
		if (HouQiao_SB_1 == 0)
		{

			if (ECU.FeEL[11] == 1)
			{
				SiLun_Fa = 1;
			}
			if (ECU.FeEL[11] == 2)
			{
				XieXing_Fa = 1;
			}
		}
		if (HouQiao_SB_1 == 1)
		{
			SiLun_Fa = 0;
			XieXing_Fa = 0;
			Si_Xie_Liang = 1;
		}
		if (Si_Xie_Liang == 1)
		{
			SL_count++;
		}
		if (SL_count >= 50)
		{
			SL_count = 0;
			if (HouQiao_SB_1 == 1)
			{
				SiLun_Fa = 0;
				XieXing_Fa = 0;
				ECU.FeEL[11] = 0;
				Si_Xie_Liang = 0;
			}
			else
			{
				if (ECU.FeEL[11] == 1)
				{
					SiLun_Fa = 1;
				}
				if (ECU.FeEL[11] == 2)
				{
					XieXing_Fa = 1;
				}
			}
		}
	}
	// 亮灯逻辑
	if (SiLun_MoShi_SB == 1)
	{
		if (SiLun_Fa != 1)
		{

			LP_SiLun = 0;
			LP_SiLun_S = 1;
			LP_XieXing = 0;
			LP_XieXing_S = 0;
			LP_LiangLun = 0;
			LP_LiangLun_S = 0;
		}
		else
		{
			LP_SiLun = 1;
			LP_SiLun_S = 0;
			LP_XieXing = 0;
			LP_XieXing_S = 0;
			LP_LiangLun = 0;
			LP_LiangLun_S = 0;
		}
	}
	else if (XieXing_MoShi_SB == 1)
	{
		if (XieXing_Fa != 1)
		{
			LP_SiLun = 0;
			LP_SiLun_S = 0;
			LP_XieXing = 0;
			LP_XieXing_S = 1;
			LP_LiangLun = 0;
			LP_LiangLun_S = 0;
		}
		else
		{
			LP_XieXing = 1;
			LP_XieXing_S = 0;
			LP_SiLun = 0;
			LP_SiLun_S = 0;
			LP_LiangLun = 0;
			LP_LiangLun_S = 0;
		}
	}
	else
	{
		if (SiLun_Fa || XieXing_Fa)
		{
			LP_XieXing = 0;
			LP_XieXing_S = 0;
			LP_SiLun = 0;
			LP_SiLun_S = 0;
			LP_LiangLun = 0;
			LP_LiangLun_S = 1;
		}
		else
		{
			LP_XieXing = 0;
			LP_XieXing_S = 0;
			LP_SiLun = 0;
			LP_SiLun_S = 0;
			LP_LiangLun = 1;
			LP_LiangLun_S = 0;
		}
	}
	/*****************************
	 * @file 换挡
	 * @brief 液力变矩器换挡逻辑
	 *****************************/
	// 发动机启动保护
	if (DW_QianJin || DW_HouTui)
	{
		Eng_Start_Protect = 1;
	}
	else
	{
		Eng_Start_Protect = 0;
	}
	// 换挡程序
	if (R_Trig(DangWei_N, &DW_NMID))
	{
		DW_QianJin = 0;
		DW_HouTui = 0;
		DW_YS = 0;
		DW_YS1 = 0;
		DW_YS_count = 0;
		DW_YS_count1 = 0;
	}
	if (R_Trig(DangWei_QianHou_mV > 3500, &DW_MID1))
	{
		// 挂入前进挡时，如果此时在后退档上，进入延时状态
		if (DW_HouTui)
		{
			DW_QianJin = 0;
			DW_HouTui = 0;
			DW_YS = 1;
		}
		else
		{
			DW_QianJin = 1;
			DW_HouTui = 0;
		}
	}
	else if (R_Trig(DangWei_QianHou_mV < 1500, &DW_MID2))
	{
		if (DW_QianJin)
		{
			DW_QianJin = 0;
			DW_HouTui = 0;
			DW_YS1 = 1;
		}
		else
		{
			DW_QianJin = 0;
			DW_HouTui = 1;
		}
	}
	else if (DangWei_QianHou_mV > 4900 || DangWei_QianHou_mV < 300)
	{
		DW_QianJin = 0;
		DW_HouTui = 0;
		DW_YS = 0;
		DW_YS1 = 0;
		DW_YS_count = 0;
		DW_YS_count1 = 0;
	}

	if (DW_YS)
	{
		DW_YS_count++;
		// 延时800mm后，前进挡挂入
		if (DW_YS_count == 60)
		{
			DW_YS = 0;
			DW_YS_count = 0;
			DW_QianJin = 1;
			DW_HouTui = 0;
		}
	}
	if (DW_YS1)
	{
		DW_YS_count1++;
		// 延时600mm后，后退挡挂入
		if (DW_YS_count1 == 60)
		{
			DW_YS1 = 0;
			DW_YS_count1 = 0;
			DW_QianJin = 0;
			DW_HouTui = 1;
		}
	}
	/*
		if (R_Trig(Eng_Speed > 0, &MID7))
		{
			DW_QianJin = 0;
			DW_HouTui = 0;
			DW_YS = 0;
			DW_YS1 = 0;
			DW_YS_count = 0;
			DW_YS_count1 = 0;
		}
	*/
	if (DiSu_MoShi)
	{
		if (DW_QianJin)
		{
			D1 = 1;
			D2 = 0;
			N = 0;
			R1 = 0;
			R2 = 0;
		}
		else if (DW_HouTui)
		{
			D1 = 0;
			D2 = 0;
			N = 0;
			R1 = 1;
			R2 = 0;
		}
		else
		{
			D1 = 0;
			D2 = 0;
			N = 1;
			R1 = 0;
			R2 = 0;
		}
	}
	else
	{
		if (DW_QianJin)
		{
			D1 = 0;
			D2 = 1;
			N = 0;
			R1 = 0;
			R2 = 0;
		}
		else if (DW_HouTui)
		{
			D1 = 0;
			D2 = 0;
			N = 0;
			R1 = 0;
			R2 = 1;
		}
		else
		{
			D1 = 0;
			D2 = 0;
			N = 1;
			R1 = 0;
			R2 = 0;
		}
	}
	QianJin_Valve = DW_QianJin;
	HouTui_Valve = DW_HouTui;
	/*低速模式*/
	if (R_Trig(DangWei_M, &DiSu_MID))
	{
		if (DiSu_MoShi == 1)
		{
			DiSu_MoShi = 0;
		}
		else
		{
			DiSu_MoShi = 1;
		}
	}
	GaoDiSu_Valve = DiSu_MoShi;
	/*****************************
	 * @file 上装
	 * @brief 电比例上装逻辑
	 *****************************/
	/*大臂变幅*/
	if (ShouBing_Y_mV <= 2300 && ShouBing_Y_mV > 100)
	{
		Valve_Current_Boom_UP = 0;
		if (ShouBing_Y_mV <= 1700 && ShouBing_Y_mV > 100)
		{
			Valve_Current_Boom_DOWN = SCALE_U16(Dec_Option(ShouBing_Y_mV, 3, 10, ARR1), 2300, 500, 380, 750);
		}
		else
		{
			Valve_Current_Boom_DOWN = SCALE_U16(ShouBing_Y_mV, 2300, 500, 380, 750);
		}
	}
	else if (ShouBing_Y_mV >= 2750 & ShouBing_Y_mV < 4900)
	{
		Valve_Current_Boom_UP = SCALE_U16(ShouBing_Y_mV, 2750, 4600, 320, 700);
		Valve_Current_Boom_DOWN = 0;
	}
	else
	{
		Valve_Current_Boom_UP = 0;
		Valve_Current_Boom_DOWN = 0;
	}
	Boom_UP = (32767 * Valve_Current_Boom_UP * 21) / System_Voltage;
	Boom_DOWN = (32767 * Valve_Current_Boom_DOWN * 21) / System_Voltage;
	// 大臂下降时打开平衡阀切断阀
	if (Valve_Current_Boom_DOWN > 400)
	{
		PingHengFa_QD = 1;
	}
	else
	{
		PingHengFa_QD = 0;
	}

	/*大臂伸缩*/
	if (ShenSuo_BL_mV <= 2400 && ShenSuo_BL_mV > 100)
	{
		Valve_Current_Boom_Shen = 0;
		Valve_Current_Boom_Suo = SCALE_U16(ShenSuo_BL_mV, 2400, 500, 450, 800);
	}
	else if (ShenSuo_BL_mV >= 2700 && ShenSuo_BL_mV < 4900)
	{
		Valve_Current_Boom_Shen = SCALE_U16(ShenSuo_BL_mV, 2700, 4600, 450, 800);
		Valve_Current_Boom_Suo = 0;
	}
	else
	{
		Valve_Current_Boom_Shen = 0;
		Valve_Current_Boom_Suo = 0;
	}
	Boom_Shen = (32767 * Valve_Current_Boom_Shen * 21) / System_Voltage;
	Boom_Suo = (32767 * Valve_Current_Boom_Suo * 21) / System_Voltage;
	/*翻斗收斗*/
	if (ShouBing_X_mV <= 2300 && ShouBing_X_mV > 100)
	{
		Valve_Current_FanDou = 0;
		Valve_Current_ShouDou = SCALE_U16(ShouBing_X_mV, 2400, 500, 380, 680);
	}
	else if (ShouBing_X_mV >= 2750 && ShouBing_X_mV < 4900)
	{
		Valve_Current_FanDou = SCALE_U16(ShouBing_X_mV, 2750, 4500, 380, 630);
		Valve_Current_ShouDou = 0;
	}
	else
	{
		Valve_Current_FanDou = 0;
		Valve_Current_ShouDou = 0;
	}
	FanDou_UP = (32767 * Valve_Current_ShouDou * 21) / System_Voltage;
	FanDou_DOWN = (32767 * Valve_Current_FanDou * 21) / System_Voltage;
	/*辅具*/
	if (FuJu_BL_mV <= 2400 && FuJu_BL_mV > 100)
	{
		Valve_Current_FuJu_a = 0;
		Valve_Current_FuJu_b = SCALE_U16(FuJu_BL_mV, 2400, 500, 450, 750);
	}
	else if (FuJu_BL_mV >= 2700 && FuJu_BL_mV < 4900)
	{
		Valve_Current_FuJu_a = SCALE_U16(FuJu_BL_mV, 2700, 4600, 450, 750);
		Valve_Current_FuJu_b = 0;
	}
	else
	{
		Valve_Current_FuJu_a = 0;
		Valve_Current_FuJu_b = 0;
	}
	FuJu_Valve_a = (32767 * Valve_Current_FuJu_a * 21) / System_Voltage;
	FuJu_Valve_b = (32767 * Valve_Current_FuJu_b * 21) / System_Voltage;

	/*===================输出刷新========================*/
	ECU.PIN_2.PWM = FuJu_Valve_a;
	ECU.PIN_10.PWM = Boom_UP;
	ECU.PIN_11.PWM = Boom_DOWN;
	ECU.PIN_12.PWM = FanDou_UP;
	ECU.PIN_13.PWM = FanDou_DOWN;
	ECU.PIN_25.PWM = Boom_Shen;
	ECU.PIN_26.PWM = Boom_Suo;
	ECU.PIN_29.PWM = FuJu_Valve_b;
	ECU.PIN_38.DO = PingHengFa_QD;
	ZHD_024_OUT.PIN_7.DO = SiLun_Fa;
	ZHD_024_OUT.PIN_8.DO = XieXing_Fa;
	ZHD_024_OUT.PIN_14.DO = QianJin_Valve;
	ZHD_024_OUT.PIN_15.DO = HouTui_Valve;
	ZHD_024_OUT.PIN_16.DO = GaoDiSu_Valve;
	ZHD_024_OUT.PIN_22.DO = Eng_Start_Protect;
	/*=======================CAN发送========================*/
	Tx_020[0] = ZHD_024_OUT.PIN_7.DO | ZHD_024_OUT.PIN_8.DO << 1 | ZHD_024_OUT.PIN_14.DO << 2 | ZHD_024_OUT.PIN_15.DO << 3 | ZHD_024_OUT.PIN_16.DO << 4 | ZHD_024_OUT.PIN_22.DO << 5;
	Tx_020[1] = 0;
	Tx_020[2] = 0;
	Tx_020[3] = 0;
	Tx_020[4] = 0;
	Tx_020[5] = 0;
	Tx_020[6] = 0;
	Tx_020[7] = 0;

	Tx_SL[0] = LP_LiangLun + LP_LiangLun_S * 2 + LP_SiLun * 4 + LP_SiLun_S * 8 + LP_XieXing * 16 + LP_XieXing_S * 32;
	Tx_SL[1] = D1 + D2 * 2 + N * 4 + R1 * 8 + R2 * 16;
	Tx_SL[2] = 0;
	Tx_SL[3] = 0;
	Tx_SL[4] = 0;
	Tx_SL[5] = 0;
	Tx_SL[6] = 0;
	Tx_SL[7] = 0;

	CAN_n++;
	if (CAN_n == 10)
	{
		CAN_Send_Msg_FC(1, 0x01, CAN_Id_Standard, 8, Tx_020);
		CAN_Send_Msg_FC(2, 0x020, CAN_Id_Standard, 8, Tx_020);
		CAN_Send_Msg_FC(2, 0x18FDDAD6, CAN_Id_Extended, 8, Tx_SL);
		CAN_n = 0;
	}
}
// #########################4.CAN1接收单元#############################
void KOP_CAN1_Event(void)
{
	CanRxMsg RxMessage;
	CAN_Receive(CAN1, 0, &RxMessage);
}
// #########################5.CAN2接收单元#############################
void KOP_CAN2_Event(void)
{
	CanRxMsg RxMessage;
	CAN_Receive(CAN2, 0, &RxMessage);
	if (RxMessage.StdId == 0x011)
	{
		ZHD_024_IN.PIN_12.DI = RxMessage.Data[0] & 1;
		ZHD_024_IN.PIN_13.DI = RxMessage.Data[0] >> 1 & 1;
		ZHD_024_IN.PIN_18.DI = RxMessage.Data[0] >> 2 & 1;
		ZHD_024_IN.PIN_19.DI = RxMessage.Data[0] >> 3 & 1;
		ZHD_024_IN.PIN_20.DI = RxMessage.Data[0] >> 4 & 1;
	}
}

/**
 * @brief  减速斜坡函数
 * @param in 输入信号
 * @param par_dec
 * @param par_delay
 * @param p
 * @return u16
 */
u16 Dec_Option(u16 in, u16 par_dec, u16 par_delay, u16 *p)
{
#define in_m *p
#define cyc_m2 *(p + 1)
#define out_m *(p + 2)
	//
	if (in >= in_m)
	{
		in_m = in;
		out_m = in;
		cyc_m2 = 0;
		return in;
	}
	else
	{
		cyc_m2 = cyc_m2 + 1;
		if (cyc_m2 >= par_delay)
		{
			// if((abs((int)in-(int)in_m))>par_dec)
			if ((in_m - in) > par_dec)
			{
				in_m = in_m - par_dec;
				cyc_m2 = par_delay;
			}
			else
			{
				in_m = in;
				cyc_m2 = 0;
			}
		}
		return in_m;
	}
}
/*****************************************************************

					掉电保存变量规划

******************************************************************
* ECU.FeEL[11]									四轮转向模式保存
*******************************************************************/
